#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# Project: spd-sxmcc
"""
@author: lyndon
@time Created on 2019/2/27 10:16
@desc
"""

import os
import time
import requests


PEER_CONF = "cache_peer %s parent %s 0 no-query weighted-round-robin weight=1 connect-fail-limit=2 allow-miss max-conn=5"

def update_conf(proxies):

    with open('/etc/squid/squid.conf.original', 'r') as F:
        squid_conf = F.readlines()
        squid_conf.append('# Cache peer config')

    for proxy in proxies:
        squid_conf.append(PEER_CONF % (proxy[0], proxy[1]))

    with open('/etc/squid/squid.conf', 'w') as F:
        F.writelines(squid_conf)

def get_proxy():
    res = requests.get(api_url).content
    if len(res) == 0:
        print('no data')
    elif 'bad' in res:
        print('bad request')
    else:
        print('get all proxies')
    proxies = []
    for line in res.split():
        proxies.append(line.strip().split(':'))